import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

public class StreamCreateTest {
    public static void main(String[] args) {
        //create stream from value
        Stream<String> stringStream = Stream.of("create", "stream", "from", "value");
        List<String> uniqueCharacter = stringStream
                .map(s -> s.split(""))
                .flatMap(Arrays::stream)
                .distinct()
                .collect(toList());
        System.out.println("unique charaters: " + uniqueCharacter);
        System.out.println("*****************************************************");

        //create stream from arrays
        int[] numbers = {1, 3, 9, 3, 6, 8, 4, 0, 1, 3};
        int sum = Arrays.stream(numbers).limit(5).sum();
        System.out.println("sum of top 5 numbers: " + sum);
        System.out.println("*****************************************************");

        //create stream from file
        List<String> uppercaseWorlds = null;
        try {
            Stream<String> lines = Files.lines(Paths.get("src/main/resources/testfile/test.txt"), Charset.defaultCharset());
            uppercaseWorlds = lines.flatMap(line -> Arrays.stream(line.split(" ")))
                    .map(String::toUpperCase)
                    .collect(toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("uppercase words " + uppercaseWorlds);
        System.out.println("*****************************************************");


        //create unlimited stream with iterate
        Stream.iterate(new int[]{0,1},t -> new int[]{t[1],t[1]+t[0]})
                .limit(10)
                .map(t->t[0])
                .forEach(s->System.out.print(s+" "));
        System.out.println("");
        System.out.println("*****************************************************");

        Stream<int[]> fibonacci = Stream.iterate(new int[]{0,1}, t -> new int[]{t[1],t[1]+t[0]})
                .limit(10);
        fibonacci.map(s->s[0]).forEach(s->System.out.print(s+" "));
        System.out.println("");
        System.out.println("*****************************************************");


        //Create unlimited stream with generate
        Stream.generate(Math::random)
                .limit(10)
                .map(s->Math.round(s*10))
                .forEach(s -> System.out.print(s+" "));
        System.out.println("");
        System.out.println("*****************************************************");
    }
}
